package com.wimcorp.magic.behaviour.attack;

import javax.persistence.Entity;

import com.wimcorp.magic.behaviour.Behaviour;
import com.wimcorp.magic.behaviour.attack.AttackBehaviour.AttackType;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.players.Player;

@Entity
public class DoubleStrike extends AttackBehaviour {

	private static DoubleStrike instance = new DoubleStrike();

	protected DoubleStrike() {

	}

	public DoubleStrike(Creature creature, AttackBehaviour attackBehaviour) {
		super(creature, attackBehaviour);
	}

	public DoubleStrike(Creature creature) {
		super(creature, null);
	}

	public static DoubleStrike instance() {
		return instance;
	}

	protected void attack(Creature defender) {
		int power = getCreature().getActualPower();
		getAttackBehaviour().attack(defender);

		if (defender.hasBehaviour(FirstStrike.instance())) {
			defender.counter(getCreature());
			if (defender.isAlive() && getCreature().isAlive()) {
				getCreature().setActualPower(power);
				getAttackBehaviour().attack(defender);
			}
		} else if (defender.hasBehaviour(DoubleStrike.instance())) {
			int defenderPower = defender.getActualPower();
			defender.counter(getCreature());

			if (defender.isAlive() && getCreature().isAlive()) {
				defender.setActualPower(defenderPower);
				getCreature().setActualPower(power);
				getAttackBehaviour().attack(defender);
				defender.counter(getCreature());
			}
		} else if (defender.isAlive()) {
			getCreature().setActualPower(power);
			getAttackBehaviour().attack(defender);
			defender.counter(getCreature());
		}

	}

	@Override
	public void counter(Creature attacker) {
		getAttackBehaviour().counter(attacker);
	}

	protected void attack(Player defender) {
		defender.subtractLife(getCreature().getActualPower());
		defender.subtractLife(getCreature().getActualPower());
		getCreature().setActualPower(0);
	}

	public int priority() {
		return AttackType.FIRST_STRIKE.ordinal();
	}

	public boolean equals(Object o) {
		return o instanceof DoubleStrike;
	}
}
